package com.joyoung.sweep.activity.login;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPStaticUtils;
import com.joyoung.sweep.R;
import com.joyoung.sweep.base.BaseActivity;
import com.joyoung.sweep.config.Cons;
import com.joyoung.sweep.utils.PermissionUtils;
import com.joyoung.sweep.widget.ProtocolDialog;

import java.util.concurrent.ThreadPoolExecutor;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: ThinkPad
 * date: 2021/4/22
 * desc: 启动页
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.iv_pic)
    ImageView ivPic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {
            finish();
            return;
        }
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        initView();
    }


    private void initView() {
        new PermissionUtils(mContext, PermissionUtils.ALL) {
            @Override
            public void onSuccess() {
                if (SPStaticUtils.getBoolean(Cons.FIRST, true)) {
                    showCenterTip();
                } else {
                    showDelayNext();
                }
            }
        };
    }

    private void showCenterTip() {
        if (!SPStaticUtils.getBoolean(Cons.AGREE)) {
            showProtocol();
        } else {
            openAct(GuideActivity.class);
            finish();
        }
    }

    private void showProtocol() {
     ProtocolDialog.getInstance(mContext).showDialog(new ProtocolDialog.DialogCallback() {
         @Override
         public void sure() {
             SPStaticUtils.put(Cons.AGREE,true);
             showDelayNext();
         }

         @Override
         public void cancel() {
            finish();
         }

         @Override
         public void protocol() {

         }

         @Override
         public void privacy() {

         }
     });
    }

    private void showDelayNext() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                openAct(LoginActivity.class);
                finish();
            }
        }, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ivPic != null) {
            ivPic.setBackgroundResource(0);
        }
    }

}